package com.atstudy.headline.web.admin.controller.statistics;

import com.atstudy.headline.entity.UserBehaviorDaily;
import com.atstudy.headline.result.Result;
import com.atstudy.headline.web.admin.service.UserBehaviorCategoryStatsService;
import com.atstudy.headline.web.admin.service.UserBehaviorDailyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/userRetentionStats")
public class UserBehaviorDailyController {

    @Autowired
    private UserBehaviorDailyService userBehaviorDailyService;

    @Autowired
    private UserBehaviorCategoryStatsService userBehaviorCategoryStatsService;

    /**
     * 分页查询用户行为数据
     * @param current 当前页码
     * @param size 每页大小
     * @param userBehaviorDaily 查询条件
     * @return 分页结果
     */
    @GetMapping("/daily/page")
    public Result<IPage<UserBehaviorDaily>> pageUserBehaviorDaily(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "6") Integer size,
            UserBehaviorDaily userBehaviorDaily) {
        Page<UserBehaviorDaily> page = new Page<>(current, size);
        LambdaQueryWrapper<UserBehaviorDaily> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(userBehaviorDaily.getViewCount() != null, UserBehaviorDaily::getViewCount, userBehaviorDaily.getViewCount());
        queryWrapper.eq(userBehaviorDaily.getClickCount() != null, UserBehaviorDaily::getClickCount, userBehaviorDaily.getClickCount());
        queryWrapper.eq(userBehaviorDaily.getStatDate() != null, UserBehaviorDaily::getStatDate, userBehaviorDaily.getStatDate());
        queryWrapper.eq(userBehaviorDaily.getDurationSeconds() != null, UserBehaviorDaily::getDurationSeconds, userBehaviorDaily.getDurationSeconds());
        IPage<UserBehaviorDaily> userBehaviorDailyIPage = userBehaviorDailyService.page(page, queryWrapper);
        return Result.ok(userBehaviorDailyIPage);
    }
    /**
     * 获取用户行为汇总数据
     * @return 用户行为汇总列表
     */
    @GetMapping("/daily/summary")
    public Result<List<Map<String, Object>>> getUserBehaviorSummary() {
        List<Map<String, Object>> summaryList = userBehaviorDailyService.getUserBehaviorSummary();
        return Result.ok(summaryList);
    }

    /**
     * 获取用户行为分类统计
     * @return 用户行为分类统计列表
     */
    @GetMapping("/category/stats")
    public Result<IPage<Map<String, Object>>> getBehaviorStatsByPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "6") Integer size) {

        Page<Map<String, Object>> page = new Page<>(current, size);
        IPage<Map<String, Object>> statsPage = userBehaviorCategoryStatsService.UserBehaviorStatsByUser(page);
        return Result.ok(statsPage);
    }
}